<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Member_model extends CI_Model {

    public function getMember() {
        $query = $this->db->get('member', 10);
        return $query->result_array();
    }

    public function getMemberById($id) {
        $query = $this->db->get_where('member', array('id' => $id), 1);
        $result = $query->result_array();
        return $result[0];
    }

    public function updateMemberById($param) {
        if (!empty($param['pin']))
            $data['pin'] = $param['pin'];
        if (!empty($param['referral_uid']))
            $data['referral_uid'] = $param['referral_uid'];
        if (!empty($param['name']))
            $data['name'] = $param['name'];
        if (!empty($param['gender']))
            $data['gender'] = $param['gender'];
        if (!empty($param['ktp']))
            $data['ktp'] = $param['ktp'];
        if (!empty($param['birth_date']))
            $data['birth_date'] = $param['birth_date'];
        if (!empty($param['birth_place']))
            $data['birth_place'] = $param['birth_place'];
        if (!empty($param['email']))
            $data['email'] = $param['email'];
        if (!empty($param['address']))
            $data['address'] = $param['address'];
        if (!empty($param['postal_code']))
            $data['postal_code'] = $param['postal_code'];
        if (!empty($param['province']))
            $data['province'] = $param['province'];
        if (!empty($param['mother_name']))
            $data['mother_name'] = $param['mother_name'];
        if (!empty($param['phone']))
            $data['phone'] = $param['phone'];
        if (!empty($param['bank_name']))
            $data['bank_name'] = $param['bank_name'];
        if (!empty($param['bank_branch']))
            $data['bank_branch'] = $param['bank_branch'];
        if (!empty($param['bank_account']))
            $data['bank_account'] = $param['bank_account'];
        if (!empty($param['bank_username']))
            $data['bank_username'] = $param['bank_username'];
        if (!empty($param['heir_name']))
            $data['heir_name'] = $param['heir_name'];
        if (!empty($param['heir_relation']))
            $data['heir_relation'] = $param['heir_relation'];
        if (!empty($param['status']))
            $data['status'] = $param['status'];
        $data['date_modified'] = date('Y-m-d H:i:s');
        
        $this->db->where('uid', $param['uid']);
        return $this->db->update('member', $data);
    }

    public function insertMember($param) {
        $data = array(
            'username' => $param['username'],
            'password' => $param['password'],
            'status' => $param['status'],
            'created' => date('Y-m-d H:i:s')
        );

        return $this->db->insert('member', $data);
    }

    public function deleteMemberById($id) {
        return $this->db->delete('member', array('id' => $id));
    }

}